package com.ds.server.support;

import org.lwjgl.util.vector.Vector3f;

// the Car class used in the server side
public class ServerCar {
	private float speed = 0f;
	private float x, y;
	private float width = 50f;
	private float height = 50f;
	private int currentIndex = 1;
	private int carType = 0;
	private boolean isCollision = false;
	public boolean moving;
	private int loop = 1;
	private int preState = 1; // 1 for zone at bottom; 2 for zone at top
	private int curState = 1;
	public boolean point1 = false;
	public boolean point2 = false;
	public boolean point3 = false;
	public boolean point4 = false;
	public boolean point5 = false;
	public boolean point6 = false;
	public float maxHp = 5f;
	public float currentHp = 5f;
	private Vector3f newPos = new Vector3f(0f, 0f, 0f);

	public ServerCar() {
		setCurrentIndex(1);
		speed = 0.0f;
		this.moving = true;
	}
	
	/**
	 * get a new position(for rmi use)
	 * 
	 * @param x
	 * @param y
	 */
	public void updatePos(Vector3f newPos) {
		setX(newPos.x);
		setY(newPos.y);
		currentIndex = (int) newPos.z;
	}

	public void goForward() {
		calPos("forward");
	}

	public void goBackward() {
		calPos("back");
	}

	public void leftRotate() {
		calPos("left");
	}

	public void rightRotate() {
		calPos("right");
	}
	
	

	/**
	 * This function updates the car's new position
	 * 
	 * @param pos
	 *            (x: x increment, y: y increment, z: new image index)
	 * @return
	 */
	public void calPos(String pos) {
		if (pos.equals("left") && this.moving == true) {
			if (currentIndex > 1) {
				currentIndex--;
			} else {
				currentIndex = 16;
			}
			carMove(speed);
		} else if (pos.equals("right") && this.moving == true) {
			if (currentIndex < 16) {
				currentIndex++;
			} else {
				currentIndex = 1;
			}
			carMove(speed);
		} else if (pos.equals("forward") && this.moving == true) {
			carMove(speed);
			if (speed < 30f)
				speed += 0.05f;
		} else if (pos.equals("back")){
			if (this.moving == true) {
				carMove(speed);
				speed = (speed > 0) ? speed - 0.5f : 0f;
			} else {
				speed = 0f;
				carMove(speed);
			}
		} else {
			stand();
		}
	}

	// calculate the car's next position then update
	public void carMove(float speed) {
		int angle = index2Angle();
		double degrees = 90 - angle * 22.5;
		double radians = Math.toRadians(degrees);
		float xIncrement = (float) (speed * Math.cos(radians));
		float yIncrement = (float) (speed * Math.sin(radians));
		this.x += xIncrement;
		this.y += yIncrement;
		setNewPos(xIncrement, yIncrement, currentIndex);
	}

	/**
	 * Transfer image index to angle index
	 * @return
	 */
	public int index2Angle() {
		return currentIndex - 1;
	}

	// gradually drop the car's speed if there is no input
	public void stand() {
		if (this.moving == true) {
			if (speed > 0) {
				speed -= 0.1f;
				carMove(speed);
			} else {
				speed = 0;
				carMove(speed);
			}
		} else {
			speed = 0;
			carMove(speed);
		}
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public Vector3f getNewPos() {
		return newPos;
	}

	public void setNewPos(float x, float y, float z) {
		this.newPos = new Vector3f(x, y, z);
	}
	
	public float getSpeed() {
		return speed;
	}
	
	public float getWidth() {
		return width;
	}
	
	public float getHeight() {
		return height;
	}	

	public void setSpeed(float speed) {
		this.speed = speed;
	}

	public int getCarType() {
		return this.carType;
	}
	
	public void setCarType(int carType) {
		this.carType = carType;
	}
	
	public boolean getCollisionStatus() {
		return this.isCollision;
	}
	
	public void setCollisionStatus(boolean isCollision) {
		this.isCollision = isCollision;
	}
	
	public Vector3f getPos() {
		return new Vector3f(this.x, this.y, (float)this.currentIndex);
	}
	
	public void setLoop(int i){//1 for increase; 0 for decrease
		if(i==1){
			loop++;
		}
		else{
			loop--;
		}
	}
	
	public void setLoopOffLine(int i){//1 for increase; 0 for decrease
		this.loop=i;
	}
	
	public int getLoop(){
		return loop;
	}
	
	public void setPreState(int i){
		preState=i;
	}
	
	public int getPreState(){
		return preState;
	}
	
	public void setCurState(int i){
		curState=i;
	}
	
	public int getCurState(){
		return curState;
	}
	
	public int getCurrentIndex() {
		return currentIndex;
	}

	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}
}	